package com.smart.manager.pop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.pop.entities.body.RPopExchange;
import com.smart.manager.pop.entities.models.PopDisExchange;
import com.smart.manager.pop.repository.PopExchangeRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "兑换码管理")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "pop/dis/exchange")
@RequiredArgsConstructor
public class ExchangeApi {

    private final PopExchangeRepository popExchangeRepository;


    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询兑换码")
    @SaCheckPermission("pop:dis:exchange:pageList")
    public AjaxResult<Page<PopDisExchange>> pageList(@Validated RPopExchange.SearchBody body) {
        Page<PopDisExchange> result = popExchangeRepository.pageList(body);
        return AjaxResult.data(result);
    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增兑换码")
    @SaCheckPermission("pop:dis:exchange:add")
    public AjaxResult<String> add(@RequestBody @Validated RPopExchange.GenerationBody body) {
        popExchangeRepository.add(body);
        return AjaxResult.data("正在生成请耐心等待!");
    }

    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除兑换码")
    @SaCheckPermission("pop:dis:exchange:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = popExchangeRepository.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }


}
